package org.noip.milhous.hierarchy;

import java.util.List;

import org.noip.milhous.DatabaseRow;
import org.noip.milhous.Database;

public class Heiarchy<ClassType extends DatabaseRow>
{
	List<ClassType> tempChildren = null;

	Database database;

	TableNode node;

	DatabaseRow parent;

	public Heiarchy(TableNode nodeConfig)
		{
			this(nodeConfig, null);
		}

	public Heiarchy(TableNode nodeConfig, Database database)
		{
			this.node = nodeConfig;
			this.database = database;
			this.parent = null;
		}

	public Heiarchy(DatabaseRow parent, TableNode nodeConfig, Database database)
		{
			this.node = nodeConfig;
			this.database = database;
			this.parent = parent;
		}

	public List<ClassType> getChildren(Database database, Object... param)
		{
			this.database = database;
			return getChildren(param);
		}

	public List<ClassType> getChildren(Object... param)
		{

			if (param.length > 0)
				{
					tempChildren = database.select(node.getClassName(), node.getWhere(), param);
				}
			else if (node.getWhere() != null) // we have an explicit where
				{
					tempChildren = database.selectByForeignKey(node.getClassName(), parent, node.getWhere());
				}
			else
				// use foreign key
				{
					tempChildren = database.selectByForiegnKey(node.getClassName(), parent);
				}

			// for (ClassType t : tempChildren)
			// {
			// // System.out.println(t + "\n");
			// for (TableNode childNode : node.getChildren())
			// {
			// Heiarchy<Children> h = new Heiarchy<Children>(t, childNode, database);
			// List<Children> kids = h.getChildren();
			// for (Children kid : kids)
			// {
			//
			// t.addChild(kid);
			//
			// }
			// }
			// }
			return tempChildren;
		}

}
